/*
Source : https://leetcode.com/problems/combinations/
Author : nflush@outlook.com
Date   : 2016-07-26
*/

/*
77. Combinations

    Total Accepted: 83290
    Total Submissions: 233169
    Difficulty: Medium

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example,
If n = 4 and k = 2, a solution is:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

Subscribe to see which companies asked this question
*/
class Solution {
private:
    void helper(int x, int start, int n, int k, vector<int> &loop, vector<vector<int>> &ret){
        for (int i = start; i <= n; i++){
            loop[x] = i;
            if (x == k){
                ret.push_back(loop);
            } else {
                helper(x+1, i + 1, n + 1, k, loop, ret);
            }
        }
    }
        
public:
    vector<vector<int>> combine(int n, int k) {
        vector<vector<int>> ret;
        if (n < k) return ret;
        vector<int> loop(k, 0);
        helper(0, 1, n -k +1, k - 1, loop, ret);
        return ret;
    }
};

